<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/9
 * Time: 16:34
 */

namespace Home\Controller;


class UserController extends HomeController
{

    public function index(){
        $this->meta_title = '用户管理';
        $this->accessList = M('Jurisdiction')->select();
        $this->display();
    }

    public function roleList(){
        $roles =  M('role')->select();
        $data = C('PLATFORM');
        foreach ($roles as $role){
            if(in_array($role['platform'], array_keys($data))){
                if(!isset($data[$role['platform']])){
                    $data[$role['platform']]['roles'] = array();
                }
                $data[$role['platform']]['roles'][] = $role;
            }
        }
        if ($data){
            $this->sucessJson($data);
        }else{
            $this->errorJson('加载角色列表失败');
        }
    }

    public function roleTable($limit, $offset){
        $where['platform'] = 1;
        $roles =  M('role')->where($where)->limit($offset, $limit)->select();
        foreach ($roles as &$role){
            $map['role_id'] = $role['role_id'];
            $js = M('RoleJurisdiction')->where($map)->field('jurisdiction_id')->select();
            $role['jcount'] = count($js);
            $role['js'] = array_column($js, 'jurisdiction_id');
        }
        $result['rows'] = $roles;
        $result['total'] = M('role')->where($where)->count();
        $this->ajaxReturn($result);
    }

    public function editUser($user_id = null){
        $userModel = D('User');
        if(IS_POST){
            if(empty($_POST['icon'])){
                if(isset($_POST['sex'])){
                    if($_POST['sex'] == 1){
                        $_POST['icon'] = C('DEFAULT_USER_ICON.MALE');
                    }else{
                        $_POST['icon'] = C('DEFAULT_USER_ICON.FEMALE');
                    }
                }
            }
            $_POST['orgcode'] = $_POST['group_code'];
            $res = $userModel->update();
            if($res){
                logOperation('用户管理','编辑用户');
                $this->sucessJson($res);
            }else{
                $this->errorJson($userModel->getError());
            }
        }else{
            if(empty($user_id)){
                $this->errorJson('参数不正确');
            }else{
                $info = $userModel->find($user_id);
                if($info){
                    $this->sucessJson($info);
                }else{
                    $this->errorJson($userModel->getError());
                }
            }
        }
    }

    public function updateRoleAccess(){
        if(IS_POST){
            $data['role_id'] = I('role_id');
            $data['role_name'] = I('role_name');
            $res = D('Role')->update($data);
            if($res){
                $role_id = $res['role_id'];
                if(!empty($role_id)){
                    M('RoleJurisdiction')->where("role_id={$role_id}")->delete();
                }


                $jurisdiction = $_POST['jurisdiction'];

                if($jurisdiction && count($jurisdiction) > 0){
                    $records = array();
                    foreach ($jurisdiction as $j){
                        $records[] = array('role_id'=>$role_id,'jurisdiction_id'=>$j);

                    }
                    if(M('RoleJurisdiction')->addAll($records) === false){
                        $this->errorJson('数据更新错误');
                    }
                }
                logOperation('用户管理','更新权限');
                $this->sucessJson();

            }else{
                $this->errorJson('数据修改错误');
            }

        }
        $this->errorJson('请求方法错误');
    }

    public function delete(){
        $ids = I('selectedIds');
        $where['user_id'] = array('in', $ids);
        $res = M('User')->where($where)->setField('state', 3);
        if($res !== false){
            logOperation('用户管理','删除用户');
            $this->sucessJson();
        }else{
            $this->errorJson('删除失败');
        }
    }

    public function changeState($user_id, $state){

        $where['user_id'] = $user_id;
        $res = M('User')->where($where)->setField('state', $state);
        if($res !== false){
            logOperation('用户管理','修改用户状态');
            $this->sucessJson();
        }else{
            $this->errorJson('修改失败');
        }
    }

    public function unlock($user_id){
        $where['user_id'] = $user_id;
        $res = M('User')->where($where)->setField('IMEI', '');
        if($res !== false){
            logOperation('用户管理','解锁用户');
            $this->sucessJson();
        }else{
            $this->errorJson('解锁失败');
        }
    }

    public function deleteOrg($org_id){
        $where['org_id'] = $org_id;
        $res = M('User')->where($where)->setField('org_id', 0);
        if($res !== false){
            $res = M('Organizations')->where("org_id={$org_id}")->delete();
            if($res !== false){
                M('Organizations')->where("parent_id={$org_id}")->setField('parent_id', 0);
                logOperation('用户管理','删除组织架构');
                $this->sucessJson();
            }else{
                $this->errorJson('删除失败');
            }
        }else{
            $this->errorJson('删除失败');
        }

    }

    public function addOrg($org_name, $parent_id){
        $data['org_name'] = $org_name;
        $data['parent_id'] = $parent_id;
        $model = M('Organizations');
        if($model->create($data)){
            $id = $model->add();
            if($id > 0){
                logOperation('用户管理','新增组织架构');
                $this->sucessJson();
            }
        }
        $this->errorJson('添加组织机构失败');
    }

    public function editOrg($org_name, $org_id){
        $model = M('Organizations');
        $data = $model->find($org_id);
        if($data){
            $data['org_name'] = $org_name;
            if($model->save($data) !== false){
                logOperation('用户管理','编辑组织架构');
                $this->sucessJson();
            }else{
                $this->errorJson('修改失败');
            }
        }else{
            $this->errorJson('未找到记录');
        }
    }


}